﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Phoenix.AppCode.Entities;
using System.Data.SqlClient;
using System.Data;

namespace Phoenix.AppCode.Data
{
    public class CorpInfoDAO : BaseDAO
    {
        #region Backend Method
        /// <summary>
        /// Edit 
        /// </summary>
        /// <param name="category"></param>
        public void Edit(CorpInfo corpinfo)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spCorpInfoEdit", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@CorpName_vi", corpinfo.CorpName_vi));
                command.Parameters.Add(new SqlParameter("@CorpName_en", corpinfo.CorpName_en));
                command.Parameters.Add(new SqlParameter("@Address_vi", corpinfo.Address_vi));
                command.Parameters.Add(new SqlParameter("@Address_en", corpinfo.Address_en));
                command.Parameters.Add(new SqlParameter("@Phone", corpinfo.Phone));
                command.Parameters.Add(new SqlParameter("@Fax", corpinfo.Fax));
                command.Parameters.Add(new SqlParameter("@Email", corpinfo.Email));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// Show Corparation Infomation
        /// </summary>
        /// <returns></returns>
        public DataTable CorpInfoList()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spCorpInfoList", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// To edit
        /// </summary>
        /// <returns></returns>
        public CorpInfo GetCorpInfo()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spCorpInfoList", connection);
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();
                CorpInfo CorpInfo = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                        CorpInfo = CreateCorpInfoFromReader(reader);
                    //else
                    //   throw new DataAccessException(String.Format(Resources.Strings.GetEntityException, "Category", ID));
                }

                return CorpInfo;
            }
        }

        private CorpInfo CreateCorpInfoFromReader(IDataReader reader)
        {
            CorpInfo corpnnfo = new CorpInfo();
            corpnnfo.CorpName = (string)reader["CorpName"];
            corpnnfo.Address = (string)reader["Address"];
            corpnnfo.Phone = (string)reader["Phone"];
            corpnnfo.Fax = (string)reader["Fax"];
            corpnnfo.Email = (string)reader["Email"];
            return corpnnfo;
        }
        #endregion
    }
}